﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Bridge.UI
{
    /// <summary>
    /// Interaction logic for TopCloseBar.xaml
    /// </summary>
    public partial class TopCloseBar : UserControl
    {
        #region Public Static

        #region Dependency Properties

        public static readonly DependencyProperty CanMinimizeProperty = DependencyProperty.Register(
            "CanMinimize",
            typeof(bool),
            typeof(TopCloseBar),
            new PropertyMetadata(true));

        public static readonly DependencyProperty CanMaximizeProperty = DependencyProperty.Register(
            "CanMaximize",
            typeof(bool),
            typeof(TopCloseBar),
            new PropertyMetadata(true));

        public static readonly DependencyProperty CanCloseProperty = DependencyProperty.Register(
            "CanClose",
            typeof(bool),
            typeof(TopCloseBar),
            new PropertyMetadata(true));

        public static readonly DependencyProperty HideCloseButtonProperty = DependencyProperty.Register(
            "HideCloseButton",
            typeof(bool),
            typeof(TopCloseBar),
            new PropertyMetadata(false));

        public static readonly DependencyProperty TitleTextProperty = DependencyProperty.Register(
            "TitleText",
            typeof(string),
            typeof(TopCloseBar),
            new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty WindowStateProperty = DependencyProperty.Register(
            "WindowState",
            typeof(WindowState),
            typeof(TopCloseBar),
            new PropertyMetadata(WindowState.Normal));

        #endregion

        #region Routed Events

        public static readonly RoutedEvent DragEvent = EventManager.RegisterRoutedEvent(
            "Drag",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(TopCloseBar));

        public static readonly RoutedEvent MinimizeEvent = EventManager.RegisterRoutedEvent(
            "Minimize",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(TopCloseBar));

        public static readonly RoutedEvent MaximizeEvent = EventManager.RegisterRoutedEvent(
            "Maximize",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(TopCloseBar));

        public static readonly RoutedEvent RestoreEvent = EventManager.RegisterRoutedEvent(
            "Restore",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(TopCloseBar));

        public static readonly RoutedEvent CloseEvent = EventManager.RegisterRoutedEvent(
            "Close",
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(TopCloseBar));

        #endregion

        #endregion

        #region Public

        public TopCloseBar()
        {
            InitializeComponent();
        }

        #region Events
        public event RoutedEventHandler Drag
        {
            add { AddHandler(DragEvent, value); }
            remove { RemoveHandler(DragEvent, value); }
        }

        public event RoutedEventHandler Minimize
        {
            add { AddHandler(MinimizeEvent, value); }
            remove { RemoveHandler(MinimizeEvent, value); }
        }

        public event RoutedEventHandler Maximize
        {
            add { AddHandler(MaximizeEvent, value); }
            remove { RemoveHandler(MaximizeEvent, value); }
        }

        public event RoutedEventHandler Restore
        {
            add { AddHandler(RestoreEvent, value); }
            remove { RemoveHandler(RestoreEvent, value); }
        }

        public event RoutedEventHandler Close
        {
            add { AddHandler(CloseEvent, value); }
            remove { RemoveHandler(CloseEvent, value); }
        }

        #endregion

        #region Properties

        public bool CanMinimize
        {
            get { return (bool)GetValue(CanMinimizeProperty);  }
            set { SetValue(CanMinimizeProperty, value);  }
        }

        public bool CanMaximize
        {
            get { return (bool)GetValue(CanMaximizeProperty); }
            set { SetValue(CanMaximizeProperty, value); }
        }

        public bool CanClose
        {
            get { return (bool)GetValue(CanCloseProperty); }
            set { SetValue(CanCloseProperty, value); }
        }

        public bool HideCloseButton
        {
            get { return (bool)GetValue(HideCloseButtonProperty); }
            set { SetValue(HideCloseButtonProperty, value); }
        }

        public string TitleText
        {
            get { return (string)GetValue(TitleTextProperty); }
            set { SetValue(TitleTextProperty, value); }
        }

        public WindowState WindowState
        {
            get { return (WindowState)GetValue(WindowStateProperty);}
            set { SetValue(WindowStateProperty, value); }
        }

        #endregion

        #endregion

        private void buttonMinimize_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs routedEventArgs = new RoutedEventArgs(MinimizeEvent);
            RaiseEvent(routedEventArgs);
        }

        private void buttonMaximize_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs routedEventArgs = new RoutedEventArgs(WindowState == WindowState.Maximized ? RestoreEvent : MaximizeEvent);
            RaiseEvent(routedEventArgs);
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs routedEventArgs = new RoutedEventArgs(CloseEvent);
            RaiseEvent(routedEventArgs);
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            RoutedEventArgs routedEventArgs = new RoutedEventArgs(DragEvent);
            RaiseEvent(routedEventArgs);
        }

    }
}
